﻿Public Class ComprobantePresentacionDAO

    'ComprobantePresentacionId
    'ComprobantePresentacionFecha
    'ComprobantePresentacionDescripcion
    'EmpleadoId

    Public Shared Function insertarComprobantePresentacion(ByVal ComprobantePresentacionFecha As DateTime, ByVal ComprobantePresentacionDescripcion As String, ByVal EmpleadoId As Integer, ByVal FarmaciaId As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Constante.esNulo("ComprobantePresentacionDescripcion")
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_ComprobantePresentacion"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@ComprobantePresentacionId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@ComprobantePresentacionFecha", ComprobantePresentacionFecha).DbType = DbType.DateTime
                    .AddWithValue("@ComprobantePresentacionDescripcion", ComprobantePresentacionDescripcion).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
                End With
                .Connection = cn
                Return .ExecuteScalar
            End With
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return -1
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerComprobantePresentaciones(ByVal ComprobantePresentacionId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_ComprobantePresentacion_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@ComprobantePresentacionId", ComprobantePresentacionId).DbType = DbType.Int32
            da.Fill(ds, "ComprobantePresentacion")

            Return ds.Tables("ComprobantePresentacion")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarComprobantePresentacion(ByVal ComprobantePresentacionId As Integer, ByVal ComprobantePresentacionFecha As DateTime, ByVal ComprobantePresentacionDescripcion As String, ByVal EmpleadoId As Integer, ByVal FarmaciaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_ComprobantePresentacion"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@ComprobantePresentacionId", ComprobantePresentacionId).DbType = DbType.Int32
                    .AddWithValue("@ComprobantePresentacionFecha", ComprobantePresentacionFecha).DbType = DbType.DateTime
                    .AddWithValue("@ComprobantePresentacionDescripcion", ComprobantePresentacionDescripcion).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarComprobantePresentacion(ByVal ComprobantePresentacionId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_ComprobantePresentacion"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@ComprobantePresentacionId", ComprobantePresentacionId).DbType = DbType.Int32
                    .AddWithValue("@ComprobantePresentacionFecha", Constante.FECHAVACIA).DbType = DbType.DateTime
                    .AddWithValue("@ComprobantePresentacionDescripcion", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@FarmaciaId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function ReporteComprobantePresentacion(ByVal ComprobantePresentacionId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_reporteComprobanteRecepcion", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@idComprobantePresentacion", ComprobantePresentacionId).DbType = DbType.Int32
            da.Fill(ds, "ComprobantePresentacion")
            Return ds.Tables("ComprobantePresentacion")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function filtroComprobantePresentaciones(ByVal FarmaciaId As Integer, ByVal EmpleadoId As Integer, ByVal FechaDesde As String, ByVal FechaHasta As String) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_ComprobantePresentacion_filtro", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@FechaDesde", FechaDesde).DbType = DbType.String
            da.SelectCommand.Parameters.AddWithValue("@FechaHasta", FechaHasta).DbType = DbType.String


            da.Fill(ds, "ComprobantePresentacion")

            Return ds.Tables("ComprobantePresentacion")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class
